﻿using System;
using System.Collections.Generic;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ZhiKeDuoServer
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
           // starserver(args);
        }

        private static void starserver(string[] args)
        {
            try
            {
                if (args.Length == 0)
                {
                    // 同一进程中可以运行多个用户服务。若要将
                    // 另一个服务添加到此进程中，请更改下行以
                    // 创建另一个服务对象。例如，
                    ServiceBase[] ServicesToRun;
                    ServicesToRun = new ServiceBase[] { new ZhiKeDuoService() };
                    ServiceBase.Run(ServicesToRun);


                }
                else
                {
                    #region 安装服务
                    if (args[0].ToLower() == "/i" || args[0].ToLower() == "-i")
                    {
                        try
                        {
                            string[] cmdline = { };
                            string serviceFileName = System.Reflection.Assembly.GetExecutingAssembly().Location;
                            TransactedInstaller transactedInstaller = new TransactedInstaller();
                            AssemblyInstaller assemblyInstaller = new AssemblyInstaller(serviceFileName, cmdline);
                            transactedInstaller.Installers.Add(assemblyInstaller);
                            transactedInstaller.Install(new System.Collections.Hashtable());
                        }
                        catch (Exception ex)
                        {
                            string msg = ex.Message;
                        }
                    }
                    #endregion
                    #region 删除服务
                    if (args[0].ToLower() == "/u" || args[0].ToLower() == "-u")
                    {
                        try
                        {
                            string[] cmdline = { };
                            string serviceFileName = System.Reflection.Assembly.GetExecutingAssembly().Location;
                            TransactedInstaller transactedInstaller = new TransactedInstaller();
                            AssemblyInstaller assemblyInstaller = new AssemblyInstaller(serviceFileName, cmdline);
                            transactedInstaller.Installers.Add(assemblyInstaller);
                            transactedInstaller.Uninstall(null);
                        }
                        catch (Exception ex)
                        {
                            string msg = ex.Message;
                        }
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
                LogManager.WriteLog("Error", ex.Message);
            }
        }
    }
}
